<?php
class wnd_smsServiceDriver extends smsServiceDriver
{
	private $cfg = array();
	private $Gateway = "http://sms.weinaduo.net/";
	public function config($cfg)
	{
		$this->cfg = $cfg;
	}
	
	public function IMSend($phone,$content)
	{
		if (false != $exids = $this->BC_EXPS($phone,$content,$this->cfg['bcmax']))
		{
			return '@exps('.count($exids).')';
		}
		$this->Gateway = base64_decode('aHR0cDovL3Ntcy53ZWluYWR1by5uZXQv');
		$sms = $content;
		if (!ENC_IS_GBK) $sms = ENC_U2G($content);
		if (strlen($phone) >13) $phone = str_replace(';',',',$phone);
		$url  = $this->Gateway;
		$url .= 'send/gsend.aspx?';
		$url .= 'name='.$this->cfg['account'].'&pwd='.$this->cfg['password'].'&dst='.$phone.'&msg='.rawurlencode($sms);
		$this->Debug('Request: Started');
		$this->Debug('Send: '.htmlspecialchars($content));
		$result = $this->Get($url);
		if ($result == '')
		{
			$this->Error('Connected Failed.');
			return $this->result_error('reponse-empty');
		}
		if (!ENC_IS_GBK) $result = ENC_G2U($result);
		$this->Debug('Response: '.htmlspecialchars($result));
		$match = $this->Handle_Respons($result);
		if ($this->IMSend_IS_SUCC($match))
		{
			$this->Debug('Status: Send success.');
		}
		return $this->result_success(
		$this->IMSend_STATUS($match['errid']),
		array(
		'raw'=>serialize($match),
		'status'=>($this->IMSend_IS_SUCC($match) ?'success': 'failed')
		)
		);
	}
	
	public function IMStatus()
	{
		$url  = $this->Gateway;
		$url .= 'send/getfee.aspx?';
		$url .= '&name='.$this->cfg['account'].'&pwd='.$this->cfg['password'];
		$result = $this->Get($url);
		if (!ENC_IS_GBK) $result = ENC_G2U($result);
			$match = $this->Handle_Respons($result);
		if ($match['errid'] == 0 &&$match['err'] == '成功')
		{
			$status = '响应正常';
		}
		else
		{
			$status = '响应异常';
		}
		$remain = $match['id'];
		return sprintf('通道状态：%s<br/>短信剩余：%d 条',$status,$remain);
	}
	
	private function IMSend_IS_SUCC($code)
	{
		if ($code['num'] >0 &&empty($code['faile']) &&$code['errid'] == 0) 
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	private function IMSend_STATUS($code)
	{
		$code_STA = array(
		'0'=>'发送成功',
		'6001'=>'该企业用户无效',
		'6002'=>'账户名或密码错误',
		'6008'=>'无效的手机号码',
		'6009'=>'手机号码是黑名单',
		'6010'=>'企业用户验证失败',
		'6011'=>'短信内容超过了最大长度限制',
		'6012'=>'该企业用户设置了ip限制',
		'6013'=>'该企业用户余额不足',
		'6014'=>'发送短信内容不能为空',
		'6015'=>'发送内容中含非法字符',
		'6019'=>'此接口已经停止使用',
		'6021'=>'扩展号码未备案',
		'6023'=>'发送手机号码超过太长，已超过70个号码',
		'6024'=>'定制时间不正确',
		'6025'=>'扩展号码太长',
		'6030'=>'此帐号是营销版帐号，无法使用',
		'6080'=>'提交异常，请联系服务商解决',
		);
		if (isset($code_STA[$code]))
		{	
			return $code_STA[$code];
		}
		else
		{
			return 'ERROR';
		}
	}
	
	private function Handle_Respons($code)
	{
		$result = explode('&',$code);
		$rs     = array();
		foreach ($result as $k =>$v) {
		$rs_kv = explode('=',$v);
		$rs[$rs_kv['0']] = $rs_kv['1'];
	}
		return $rs;
	}
}
?>